package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.zzgele.qmzb.api.core.Constants;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultCode;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.Bonus;
import com.zzgele.qmzb.api.model.BonusPackage;
import com.zzgele.qmzb.api.service.BonusPackageService;
import com.zzgele.qmzb.api.service.BonusService;
import com.zzgele.qmzb.api.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;

/**
* Created by Martin on 2017/07/09.
*/
@RestController
@RequestMapping("/bonus")
public class BonusController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @DubboConsumer
    private BonusPackageService bonusPackageService;
    @DubboConsumer
    private BonusService bonusService;

    @PostMapping("/create")
    public Result create(Long login_uid, Integer amount, Integer price) {
//        Integer price2=50;
        Double price2=price*(1- Constants.COMMISSION_BONUS)  ;
        //最少5个钻石 最少10个
        if(amount< Constants.RED_PACKET_LIMIT_NUMBER||price<50) {
            Result result = new Result();
            result.setCode(ResultCode.PARAM_ERROR).setMessage("参数错误");
            return result;
       }
       //校验钻石 
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MINUTE, Constants.RED_PACKET_LIMIT_MINUTE);
        int temp= DateUtil.getUnixTimeStampByDate(calendar.getTime());
        BonusPackage bonusPackage= bonusPackageService.createTotalPriceBonusPackage(amount,price2.intValue(),temp,login_uid);
        if(bonusPackage==null)
        {
            return ResultGenerator.genFailResult("砖石不足");
        }
        HashMap hashMap = new HashMap();
        hashMap.put("bonus_id",  bonusPackage.getId());
        return ResultGenerator.genSuccessResult(hashMap);
    }
    @PostMapping("/get")
    public Result get(Long login_uid, Long bonus_id) {
       Bonus bonus= bonusService.getOneBonus(login_uid,bonus_id);
        if(bonus==null){
            Result result = new Result();
            result.setCode(ResultCode.BONUS_PACKAGE_ERROR).setMessage("红包抢完了");
            return result;
        }
        if(bonus.getStatus()==602){
            Result result = new Result();
            result.setCode(ResultCode.BONUS_REPEAT_USER).setMessage("你已经抢过了");
            return result;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("price",  bonus.getPrice());
        return ResultGenerator.genSuccessResult(hashMap);

    }
}
